#! /usr/bin/python

import os, sys
from random import randint, choice
from math import sin, cos, radians

import pygame
from pygame.sprite import Sprite

from vec2d import vec2d

from actor import Actor

SCREEN_WIDTH, SCREEN_HEIGHT = 512, 512
BG_COLOR = 150, 150, 80
POINT_COLOR = 255,255,255
CREEP_FILENAMES = [
    'bluecreep.png', 
    'pinkcreep.png', 
    'graycreep.png']


N_CREEPS = 20

pygame.init()

screen = pygame.display.set_mode(
    (SCREEN_WIDTH, SCREEN_HEIGHT), 0, 32)

images = [ pygame.image.load(x).convert_alpha() for x in CREEP_FILENAMES ]

clock = pygame.time.Clock()

# Create N_ACTORS actors
actors = []    
paused = False

actors.append(Actor(screen, images[0], 
                    (SCREEN_WIDTH/2, SCREEN_HEIGHT/2), 
                    (1, 0)))
actors.append(Actor(screen, images[0],
                    (SCREEN_WIDTH/2+20, SCREEN_HEIGHT/2),
                    (1,0)))

actors[1].follow(actors[0])
actors.append(Actor(screen, images[0],
                    (SCREEN_WIDTH/2-20, SCREEN_HEIGHT/2),
                    (1,0)))

actors[2].follow(actors[0])

mpos = actors[0].pos

while True:
# 50 fps
    time_passed = clock.tick(50)
        
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                paused = not paused
            elif event.key == pygame.K_w:
                actors[0].forward_down()
 #               print 'forward down'
            elif event.key == pygame.K_s:
                actors[0].backward_down()
            elif event.key == pygame.K_a:
                actors[0].left_down()
            elif event.key == pygame.K_d:
                actors[0].right_down()
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_w:
                actors[0].forward_up()
#                print 'forward up'
            elif event.key == pygame.K_s:
                actors[0].backward_up()
            elif event.key == pygame.K_a:
                actors[0].left_up()
            elif event.key == pygame.K_d:
                actors[0].right_up()
        elif event.type == pygame.MOUSEMOTION:
            mpos = pygame.mouse.get_pos() 
# update player orientation
            actors[0].direction = vec2d(mpos[0] - actors[0].pos.x, mpos[1] - actors[0].pos.y).normalized()

    screen.fill(BG_COLOR) # draw background

# draw cursor
    pygame.draw.aaline(screen,POINT_COLOR,actors[0].pos,mpos)
    
    if not paused:
        for actor in actors:
            actor.update([],time_passed)
            actor.draw()

    
    pygame.display.flip()
